﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmInformation : FormBase
    {
        public frmInformation()
        {
            InitializeComponent();
            

            //Get Info
            DataTable dt = tbl_Parameters_Controller.Instance().CompanyGetAll();
            foreach (DataRow dr in dt.Rows)
            {
                string itemCode = dr["ItemCode"].ToString();
                object itemValue = dr["ItemValue"];
                foreach (Control ctl in this.Controls)
                {
                    if (ctl is TextBox)
                        if (ctl.Name.Substring(3).Equals(itemCode))
                        {
                            if (((TextBox)ctl).TextAlign == HorizontalAlignment.Right && Globals.IsNumeric(itemValue))
                                ctl.Text = Globals.ToDecimal(itemValue).ToString("#,0.00");
                            else
                                ctl.Text = itemValue.ToString();
                        }
                }
            }
        }

        public override void Show_Form()
        {
            this.ShowDialog();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool isSuccess = true;
            tbl_Parameters_Info _parameterInfo = new tbl_Parameters_Info();
            foreach (Control ctl in this.Controls)
            {
                if (ctl is TextBox)
                {
                    //Set Info
                    _parameterInfo.DomainCode = "Company";
                    _parameterInfo.ItemCode = ctl.Name.Substring(3);
                    _parameterInfo.ItemOrder = ctl.TabIndex;
                    if (((TextBox)ctl).TextAlign == HorizontalAlignment.Right)
                        _parameterInfo.ItemValue = Globals.ToDouble(ctl.Text).ToString();
                    else
                        _parameterInfo.ItemValue = ctl.Text;
                    
                    //Save
                    if (!tbl_Parameters_Controller.Instance().Save(_parameterInfo))
                    {
                        isSuccess = false;
                        break;
                    }
                }
            }

            if (isSuccess)
            {
                //Reload Info for System
                FormGlobals.Message_InTaskbar(UIMessage.SAVE_COMPLETE);
            }
            else
            {
                
                FormGlobals.Message_Error(new Exception(UIMessage.SAVE_ERROR));
            }

        }
    }
}
